<?php
class documentEditAction extends sfActions
{
	public function execute($request)
	{
		$this->document_id = $request->getParameter('document_id');
		if ($this->document_id) {
			$this->document = Doctrine_Core::getTable('VTNSDocument')->find($this->document_id);
			if ($this->document)
				$this->document_form = new AdminDocumentForm($this->document);
		}
        else {
            $this->document_form = new AdminDocumentForm();
        }
		if ($request->hasParameter('document')) {
			$form = $request->getParameter('document');
			
			$this->status = $this->updatedocumentFromRequest($this->document, $form);
			try {
                  $files = $request->getFiles();
                  // get attribute of file   
	              foreach($files as $file) {
                      $source = $file['file']['tmp_name'];
                      $fileName = $file['file']['name'];
                      $fileSize = $file['file']['size'];
                      $fileType = $file['file']['type'];
                  }
                  $fileExt = trim(strrchr($fileName,'.'));
                  $document = new VTNSDocument();
                  $file_name_uploadeds = $document->file_type_uploaded['name'];
                  foreach( $file_name_uploadeds as $file_upload) {
                     if( $file_upload == $fileExt ) {
                         $this->status_upload_file = true;
                         break;
                     }
                     else 
                         $this->status_upload_file = false;
                  }
                  if($fileSize > $document->file_type_uploaded['size'] || $fileSize == 0) {
                     $this->message = "File upload phải nhỏ hơn 2MB";
                     $this->status_upload_file = false;
                     echo $this->status_upload_file;
                     echo  $this->message;
                  }
                  // upload neư file
                  if($this->status_upload_file) {
                      // delete old file if exist  
                      if(isset($this->document->file) && $this->document->file != null) {
    			         $path_files =  "uploads/document/".$this->document->file;
                          if(file_exists($path_files))
                            unlink($path_files);
  			          }  
                     $uploadDir = sfConfig::get("sf_upload_dir");
                     $destination = $uploadDir.'/document/'.$fileName;
                     if( !move_uploaded_file($source,$destination)) 
                        $this->message = "upload file không thành công";
                     else {
                        $this->message = "upload file thành công";
                        $this->document->file = $fileName;
                        $this->document->file_type = $fileExt;
                        $fileSize = $fileSize/1024;
                        $this->document->file_size = round($fileSize,1);
                        $this->document->save();
                     }
                  }
			} catch (sfException $e) {
				echo $e->getMessage();
                
			}
			
			if ($this->status) {
				$this->document->save();
				$this->redirect('document/documentList?document_category_id='.$this->document->document_category_id.'&notify=editSuccess');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updatedocumentFromRequest(&$document, $form)
	{
		if ($form['content'])
			$document->content = $form['content'];
		if ($form['name'])
			$document->name = $form['name'];	
		if (isset($form['is_active']))
			$document->is_active = 1;
		else
			$document->is_active = 0;
		if ($form['description'])
			$document->description = $form['description'];

		$document->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>